package edu.scnu.fans.fingertip.item.item;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.scnu.fans.fingertip.common.api.IdRequest;
import edu.scnu.fans.fingertip.common.web.BusinessException;
import edu.scnu.fans.fingertip.item.mapper.ItemImgMapper;
import edu.scnu.fans.fingertip.item.mapper.ItemMapper;
import edu.scnu.fans.fingertip.item.mapper.ItemSpecMapper;
import edu.scnu.fans.fingertip.item.model.Item;
import edu.scnu.fans.fingertip.item.model.ItemImg;
import edu.scnu.fans.fingertip.item.model.ItemSpec;
import edu.scnu.fans.fingertip.item.output.ItemImgOutput;
import edu.scnu.fans.fingertip.item.output.ItemOutput;
import edu.scnu.fans.fingertip.item.output.ItemSpecOutput;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ItemService {

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private ItemSpecMapper itemSpecMapper;

    @Resource
    private ItemImgMapper itemImgMapper;

    /**
     * 商品列表
     */
    public List<ItemOutput> list() {
        // 商品列表
        List<ItemOutput> list = itemMapper.selectList(new LambdaQueryWrapper<Item>()
                        .eq(Item::getStatus, Item.UP_STATUS))
                .stream()
                .map(Item::transferToOutput)
                .collect(Collectors.toList());

        // 对商品数据进行完善，设置图片和规格
        this.completeItem(list);

        return list;
    }

    /**
     * 商品详情
     */
    public ItemOutput detail(IdRequest input) {
        ItemOutput item = Optional.ofNullable(itemMapper.selectOne(new LambdaQueryWrapper<Item>()
                        .eq(Item::getId, input.getId())
                        .eq(Item::getStatus, Item.UP_STATUS)))
                .map(Item::transferToOutput)
                .orElseThrow(() -> new BusinessException("商品不存在"));

        // 对商品数据进行完善，设置图片和规格
        this.completeItem(Collections.singletonList(item));

        return item;
    }

    /**
     * 对商品数据进行完善，设置图片和规格
     */
    public void completeItem(List<ItemOutput> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<Long> itemIdList = list.stream()
                .map(ItemOutput::getId)
                .collect(Collectors.toList());

        // item 与 itemImg 的映射关系
        Map<Long, List<ItemImgOutput>> imgMap = itemImgMapper.selectList(new LambdaQueryWrapper<ItemImg>()
                        .in(ItemImg::getItemId, itemIdList)
                        .orderByAsc(ItemImg::getSort))
                .stream()
                .map(ItemImg::transferToOutput)
                .collect(Collectors.groupingBy(ItemImgOutput::getItemId));

        // item 与 itemSpec 的映射关系
        Map<Long, List<ItemSpecOutput>> specMap = itemSpecMapper.selectList(new LambdaQueryWrapper<ItemSpec>()
                        .in(ItemSpec::getItemId, itemIdList))
                .stream()
                .map(ItemSpec::transferToOutput)
                .collect(Collectors.groupingBy(ItemSpecOutput::getItemId));

        // 给商品设置图片与规格
        list.forEach(item -> {
            item.setImgList(imgMap.getOrDefault(item.getId(), Collections.emptyList()));
            item.setSpecList(specMap.getOrDefault(item.getId(), Collections.emptyList()));
        });
    }

}
